﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ImageProcessingLib;

/* Урок 5. "Обработка изображений." */
 
namespace ImageProcessing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        #region === members ===

        Bitmap _bitmap;
        readonly Stopwatch _stopWatch = new Stopwatch();
        readonly ImageProcessingLib.ImageProcessing _ip = new ImageProcessingLib.ImageProcessing();

        #endregion

        #region === private methods ===

        void StartTime()
        {
            _stopWatch.Reset();
            _stopWatch.Start();
        }

        TimeSpan StopTime()
        {
            _stopWatch.Stop();
            return _stopWatch.Elapsed;
        }

        string GetElapsedTime(TimeSpan ts)
        {
            string result;

            if (ts.Minutes > 0)
                result = string.Format("{0} мин {1} сек", ts.Minutes, ts.Seconds);
            else
                result = string.Format("{0} мс", ts.Milliseconds + 1000 * ts.Seconds);

            return result;
        }

        private void UpdateUImage()
        {
            int index = cmbImages.SelectedIndex;

            if (index < 0)
                return;

            if (index == 0)
                _bitmap = Properties.Resources.BraveHeart;
            else if (index == 1)
                _bitmap = Properties.Resources.City;
            else if (index == 2)
                _bitmap = Properties.Resources.Industry;
            else if (index == 3)
                _bitmap = Properties.Resources.Moscow;
            else
                _bitmap = Properties.Resources.ColdHeart;

            pictureBox1.Image = _bitmap;
        }

        #endregion

        private void Form1_Load(object sender, EventArgs e)
        {
            cmbImages.BeginUpdate();
            cmbImages.Items.Add("Мерида");
            cmbImages.Items.Add("Город");
            cmbImages.Items.Add("Промзона");
            cmbImages.Items.Add("Москва");
            cmbImages.Items.Add("Холодное сердце");
            cmbImages.SelectedIndex = 0;
            cmbImages.EndUpdate();

            lblTimeElapsed.Text = string.Empty;

            pictureBox1.Image = _bitmap;
        }

        private void cmbImages_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateUImage();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            UpdateUImage();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StartTime();
            BitmapInfo4 bi = Utils.GetBitmapInfo(_bitmap);
            _bitmap = Utils.CreateBitmap(_ip.Brightness(bi, 20, 20, 20));
            lblTimeElapsed.Text = GetElapsedTime(StopTime());

            pictureBox1.Image = _bitmap;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            StartTime();
            BitmapInfo4 bi = Utils.GetBitmapInfo(_bitmap);
            _bitmap = Utils.CreateBitmap(_ip.Desaturate(bi));
            lblTimeElapsed.Text = GetElapsedTime(StopTime());

            pictureBox1.Image = _bitmap;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            StartTime();
            BitmapInfo4 bi = Utils.GetBitmapInfo(_bitmap);
            _bitmap = Utils.CreateBitmap(_ip.Saturate(bi, 0.5f));
            lblTimeElapsed.Text = GetElapsedTime(StopTime());

            pictureBox1.Image = _bitmap;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            StartTime();
            BitmapInfo4 bi = Utils.GetBitmapInfo(_bitmap);
            _bitmap = Utils.CreateBitmap(_ip.Blur(bi, 2));
            lblTimeElapsed.Text = GetElapsedTime(StopTime());

            pictureBox1.Image = _bitmap;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            StartTime();
            BitmapInfo4 bi = Utils.GetBitmapInfo(_bitmap);
            _bitmap = Utils.CreateBitmap(_ip.Contrast(bi, 1.15f));
            lblTimeElapsed.Text = GetElapsedTime(StopTime());

            pictureBox1.Image = _bitmap;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            StartTime();
            BitmapInfo4 bi = Utils.GetBitmapInfo(_bitmap);
            _bitmap = Utils.CreateBitmap(_ip.Diffuse(bi, 1));
            lblTimeElapsed.Text = GetElapsedTime(StopTime());

            pictureBox1.Image = _bitmap;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            StartTime();
            BitmapInfo4 bi = Utils.GetBitmapInfo(_bitmap);
            _bitmap = Utils.CreateBitmap(_ip.Invert(bi));
            lblTimeElapsed.Text = GetElapsedTime(StopTime());

            pictureBox1.Image = _bitmap;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog();

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                StartTime();
                BitmapInfo4 bi = Utils.GetBitmapInfo(_bitmap);
                _bitmap = Utils.CreateBitmap(_ip.Hue(bi, dlg.Color.R, dlg.Color.G, dlg.Color.B));
                lblTimeElapsed.Text = GetElapsedTime(StopTime());

                pictureBox1.Image = _bitmap;
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            StartTime();
            BitmapInfo4 bi = Utils.GetBitmapInfo(_bitmap);
            _bitmap = Utils.CreateBitmap(_ip.Sepia(bi));
            lblTimeElapsed.Text = GetElapsedTime(StopTime());

            pictureBox1.Image = _bitmap;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            StartTime();
            BitmapInfo4 bi = Utils.GetBitmapInfo(_bitmap);
            _bitmap = Utils.CreateBitmap(_ip.Contour(bi));
            lblTimeElapsed.Text = GetElapsedTime(StopTime());

            pictureBox1.Image = _bitmap;
        }

        private void button12_Click(object sender, EventArgs e)
        {
            StartTime();
            BitmapInfo4 bi = Utils.GetBitmapInfo(_bitmap);
            _bitmap = Utils.CreateBitmap(_ip.Relief(bi, Math.PI / 4));
            lblTimeElapsed.Text = GetElapsedTime(StopTime());

            pictureBox1.Image = _bitmap;
        }

        private void button13_Click(object sender, EventArgs e)
        {
            StartTime();
            BitmapInfo4 bi = Utils.GetBitmapInfo(_bitmap);
            _bitmap = Utils.CreateBitmap(_ip.Gamma(bi, 1.22f));
            lblTimeElapsed.Text = GetElapsedTime(StopTime());

            pictureBox1.Image = _bitmap;
        }

        private void button14_Click(object sender, EventArgs e)
        {
            StartTime();
            BitmapInfo4 bi = Utils.GetBitmapInfo(_bitmap);
            _bitmap = Utils.CreateBitmap(_ip.Emboss(bi, Math.PI / 4, 3));
            lblTimeElapsed.Text = GetElapsedTime(StopTime());

            pictureBox1.Image = _bitmap;
        }

        private void button15_Click(object sender, EventArgs e)
        {
            StartTime();
            BitmapInfo4 bi = Utils.GetBitmapInfo(_bitmap);
            _bitmap = Utils.CreateBitmap(_ip.Sharpen(bi));
            lblTimeElapsed.Text = GetElapsedTime(StopTime());

            pictureBox1.Image = _bitmap;
        }

        private void button16_Click(object sender, EventArgs e)
        {
            StartTime();
            BitmapInfo4 bi = Utils.GetBitmapInfo(_bitmap);
            _bitmap = Utils.CreateBitmap(_ip.Levels(bi, 24));
            lblTimeElapsed.Text = GetElapsedTime(StopTime());

            pictureBox1.Image = _bitmap;
        }
    }
}
